<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%--
  Created by IntelliJ IDEA.
  User: 82387
  Date: 2018/10/12
  Time: 14:01
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ include file="/WEB-INF/jsp/meta.jsp"%>
<html>
<head>
    <title></title>
</head>
<body>
<div class="block_layout block_form" id="wageInfo">
    <div class="block_title">
        <h3>人员选择</h3>
    </div>
    <div class="block_content">
        <form id="person_wage_form">
            <table cellpadding="0" cellspacing="0" class="table_form">
                <!--普通表单填写表格使用table_form-->
                <colgroup>
                    <col style="width: 50px;"/>
                    <col style="width: 100px;"/>
                    <col style="width: 50px;"/>
                    <col style="width: 100px;"/>
                </colgroup>
                <!--表单表格宽度控制使用colgroup，每个col对应下方的一个td，或者th，上下是对齐的-->
                <thead></thead>
                <!--表头-->
                <tbody>
                <!--表内容，th是蓝色字体，主要放字段名，td为字段内容-->
                <tr>
                    <c:if test="${empty wage}">
                        <th>所属部门<font color="red">*</font>:</th>
                        <td><input type="text" id="deptId" name="deptId"/></td>
                        <th>姓名<font color="red">*</font>:</th>
                        <td><input type="text" id="userName" name="userName"/></td>
                    </c:if>
                    <c:if test="${not empty wage}">
                        <th>所属部门<font color="red">*</font>:</th>
                        <td>${wage.deptName}</td>
                        <th>姓名<font color="red">*</font>:</th>
                        <td>${wage.userName}</td>
                    </c:if>
                </tr>
                </tbody>
            </table>
        </form>
    </div>
</div>
<div class="block_layout block_form">
    <div class="block_title">
        <h3>工资明细</h3>
    </div>
    <div class="block_content">
        <form id="wageInfoForm" method="post">
            <input type="hidden" value="${wage.id}" name="id"/>
            <table cellpadding="0" cellspacing="0" class="table_form">
                <!--普通表单填写表格使用table_form-->
                <colgroup>
                    <col style="width: 50px;"/>
                    <col style="width: 100px;"/>
                    <col style="width: 50px;"/>
                    <col style="width: 100px;"/>
                    <col style="width: 50px;"/>
                    <col style="width: 100px;"/>
                </colgroup>
                <!--表单表格宽度控制使用colgroup，每个col对应下方的一个td，或者th，上下是对齐的-->
                <thead></thead>
                <!--表头-->
                <tbody>
                <!--表内容，th是蓝色字体，主要放字段名，td为字段内容-->
                <tr>
                    <th>基本工资:</th>
                    <td><input type="text" name="uesrBasicwage" value="${wage.uesrBasicwage}" min="0"
                               class="easyui-validatebox" precision="2" prompt="0.00"/>
                    </td>
                    <th>薪级工资:</th>
                    <td><input class="easyui-validatebox" type="text" name="userSalaryscale"
                               value="${wage.userSalaryscale}"
                               min="0" max="500" precision="2" prompt="0.00"/></td>
                    <th>里程补贴:</th>
                    <td><input class="easyui-validatebox" type="text" name="userMiles" value="${wage.userMiles}"
                               min="0" precision="2" prompt="0.00"/></td>
                </tr>
                <tr>
                    <th>误餐补助:</th>
                    <td><input class="easyui-validatebox" type="text" name="userMisuse" value="${wage.userMisuse}"
                               min="0" precision="2" prompt="0.00"/></td>
                    <th>加班费:</th>
                    <td><input class="easyui-validatebox" type="text" name="userOvertimepay"
                               value="${wage.userOvertimepay}"
                               min="0" precision="2" prompt="0.00"/></td>
                    <th>其他1:</th>
                    <td><input class="easyui-validatebox" type="text" name="userOrther1" value="${wage.userOrther1}"
                               min="0" precision="2" prompt="0.00"/></td>
                </tr>
                <tr>
                    <th>应发工资:</th>
                    <td>
                        <c:if test="${empty wage.userPayablewage}">
                        <input class="easyui-validatebox" type="text" id="userPayablewage" name="userPayablewage"
                               min="0" disabled precision="2" value="0.00"/></td>
                    </c:if>
                    <c:if test="${not empty wage.userPayablewage}">
                        <input class="easyui-validatebox" type="text" id="userPayablewage" name="userPayablewage"
                               min="0" disabled precision="2" value="${wage.userPayablewage}"/></td>
                    </c:if>
                    <th>社保费:</th>
                    <td><input class="easyui-validatebox" type="text" name="userSocialsecurity"
                               min="0" precision="2" prompt="0.00" value="${wage.userSocialsecurity}"/></td>
                    <th>公积金:</th>
                    <td><input class="easyui-validatebox" type="text" name="userAccumulation"
                               min="0" precision="2" prompt="0.00" value="${wage.userAccumulation}"/></td>
                </tr>
                <tr>
                    <th>缺勤扣发:</th>
                    <td><input class="easyui-validatebox" type="text" name="userAbsenteeism"
                               min="0" precision="2" prompt="0.00" value="${wage.userAbsenteeism}"/></td>
                    <th>所得税:</th>
                    <td>
                        <c:if test="${not empty wage.userIncometax}">
                        <input type="text" name="userIncometax" disabled
                               min="0" class="easyui-validatebox" precision="2" value="${wage.userIncometax}"/></td>
                    </c:if>
                    <c:if test="${empty wage.userIncometax}">
                        <input type="text" name="userIncometax" disabled
                               min="0" class="easyui-validatebox" precision="2" value="0.00"/></td>
                    </c:if>

                    <th>其他2:</th>
                    <td><input class="easyui-validatebox" type="text" name="userOther2"
                               min="0" precision="2" prompt="0.00" value="${wage.userOther2}"/></td>
                </tr>
                <tr>
                    <th>月份<font color="red">*</font>:</th>
                    <td colspan="3">
                        <c:if test="${empty wage.sysDate}">
                            <input type="text" class="Wdate"
                                   onfocus="WdatePicker({dateFmt:'yyyy-MM'})" name="sysDate"/>
                        </c:if>
                        <c:if test="${not empty wage.sysDate}">
                            ${wage.sysDate}
                        </c:if>

                    </td>

                        <input type="hidden" name="carPay" value="${wage.carPay}"/>
                        <input type="hidden" name="deductions" value="${wage.deductions}"/>
                    <th>实发工资:</th>
                    <td>
                        <c:if test="${not empty wage.userNetpayroll}">
                        <input type="text" name="userNetpayroll" disabled
                               class="easyui-validatebox" precision="2" value="${wage.userNetpayroll}"/></td>
                    </c:if>
                    <c:if test="${empty wage.userNetpayroll}">
                        <input type="text" name="userNetpayroll" disabled
                               class="easyui-validatebox" precision="2" value="0.00"/></td>
                    </c:if>
                </tr>
                </tbody>
            </table>

        </form>
    </div>
</div>
<div style="float:right; margin-top:15px;margin-right: 15px">
    <a href="javascript:void(0);" class="a_blue btn_op" id="saveWage"><em>保存</em></a>
</div>
<script type="text/javascript">
    $("#saveWage").click(function () {
        if (!$("#person_wage_form").validate().form()) {
            return false;
        }

        var url;
        var userNum;
        if ($("#wageInfoForm input[name='id']").val() != null && $("#wageInfoForm input[name='id']").val() != '') {
            url = "${ctx}/wage/update"
            userNum = '';
        } else {
            if ($("#wageInfoForm input[name='sysDate']").val() == null || $("#wageInfoForm input[name='sysDate']").val() == '') {
                $.messager.alert('警告', '请填写月份');
                return false;
            }
            url = "${ctx}/wage/add";
            userNum = $("#person_wage_form input[name='userName']").val();
        }
        initUserPayablewage();
        initUserIncometax();
        initUserNetpayroll();
        ajaxLoading();
        $.post(url, $("#wageInfoForm").serialize() + "&userNum=" + userNum
            + "&userPayablewage=" + $("#wageInfoForm #userPayablewage").val()
            + "&userIncometax=" + $("#wageInfoForm input[name='userIncometax']").val()
            + "&userNetpayroll=" + $("#wageInfoForm input[name='userNetpayroll']").val(), function (data) {
            ajaxLoadEnd();
            if (data.success) {
                $.messager.alert('提示', '操作成功');
                $('#openDialog').window("close");
                $("#wageList").datagrid('reload');
            } else {
                $.messager.alert('提示', data.mes);
            }
        });
    });
    //表单验证
    $("#person_wage_form").validate({
        rules: {
            deptId: {required: true},
            userName: {required: true}
        }
    });

    $("#wageInfoForm input[name='uesrBasicwage'],input[name='userSalaryscale'],input[name='userMiles']," +
        "input[name='userMisuse'],input[name='userOvertimepay'],input[name='userOvertimepay'],input[name='userOrther1']" +
        ",input[name='userSocialsecurity'],input[name='userAccumulation']" +
        ",input[name='userAbsenteeism'],input[name='userOther2']").numberbox({
        "onChange": function () {
            initUserPayablewage();
            initUserIncometax();
            initUserNetpayroll();
        }
    });

    /**
     * 初始化应发工资函数
     */
    function initUserPayablewage() {
        var userPayablewage = Number($("#wageInfoForm input[name='uesrBasicwage']").val())
            + Number($("#wageInfoForm input[name='userSalaryscale']").val())
            + Number($("#wageInfoForm input[name='userMiles']").val())
            + Number($("#wageInfoForm input[name='userMisuse']").val())
            + Number($("#wageInfoForm input[name='userOvertimepay']").val())
            + Number($("#wageInfoForm input[name='userOrther1']").val());
        $("#wageInfoForm input[name='userPayablewage']").val(returnFloat(userPayablewage));
    }

    function initUserIncometax() {
        var userIncometax = Number($("#wageInfoForm input[name='userPayablewage']").val())
            - Number($("#wageInfoForm input[name='userSocialsecurity']").val())
            - Number($("#wageInfoForm input[name='userAccumulation']").val())
            - Number($("#wageInfoForm input[name='userAbsenteeism']").val())
            + Number($("#wageInfoForm input[name='carPay']").val())
            - Number($("#wageInfoForm input[name='deductions']").val());
        if (userIncometax > 5000) {
            userIncometax = (Number(userIncometax) - Number(5000));
            if (userIncometax >= 0 && userIncometax <= 3000) {
                userIncometax = userIncometax * 0.03;
            }
            if (userIncometax > 3000 && userIncometax <= 12000) {
                userIncometax = (userIncometax) * 0.1 - 210;
            }
            if (userIncometax > 12000 && userIncometax <= 25000) {
                userIncometax = (userIncometax) * 0.2 - 1410;
            }
            if (userIncometax > 25000 && userIncometax <= 35000) {
                userIncometax = (userIncometax) * 0.25 - 2660;
            }
            if (userIncometax > 35000 && userIncometax <= 55000) {
                userIncometax = (userIncometax) * 0.3 - 4410;
            }
            if (userIncometax > 55000 && userIncometax <= 80000) {
                userIncometax = userIncometax * 0.35 - 7160;
            }
            if (userIncometax > 80000) {
                userIncometax = (userIncometax) * 0.45 - 15160;
            }
            $("#wageInfoForm input[name='userIncometax']").val(returnFloat(userIncometax));
        } else {
            $("#wageInfoForm input[name='userIncometax']").val(returnFloat(0.00));
        }
    }

    function initUserNetpayroll() {
        var userNetpayroll = Number($("#wageInfoForm input[name='userPayablewage']").val())
            - Number($("#wageInfoForm input[name='userSocialsecurity']").val())
            - Number($("#wageInfoForm input[name='userAccumulation']").val())
            - Number($("#wageInfoForm input[name='userAbsenteeism']").val())
            - Number($("#wageInfoForm input[name='userIncometax']").val())
            - Number($("#wageInfoForm input[name='userOther2']").val());
        $("#wageInfoForm input[name='userNetpayroll']").val(returnFloat(userNetpayroll));
    }

    function returnFloat(value) {
        var value = Math.round(parseFloat(value) * 100) / 100;
        var xsd = value.toString().split(".");
        if (xsd.length == 1) {
            value = value.toString() + ".00";
            return value;
        }
        if (xsd.length > 1) {
            if (xsd[1].length < 2) {
                value = value.toString() + "0";
            }
            return value;
        }
    }

    $("#wageInfo #deptId").combobox({
        url: '${ctx}/dept/getConombox',
        valueField: 'id',
        textField: 'departName',
        panelHeight: 'auto',
        prompt: '---请选择---',
        onSelect: function (record) {
            $("#wageInfo #userName").combobox({
                url: '${ctx}/person/getPersonByDept?deptId=' + record.id,
                valueField: 'userNum',
                textField: 'userName',
                editable: true,
                panelHeight: '150px',
                filter: function (q, row) {
                    var opts = $(this).combobox('options');
                    return row[opts.textField].toLowerCase().indexOf(q.toLowerCase()) != -1;
                },
                onHidePanel: function () {
                    var valueField = $(this).combobox("options").valueField;
                    var val = $(this).combobox("getValue");  //当前combobox的值
                    var allData = $(this).combobox("getData");   //获取combobox所有数据
                    var result = true;      //为true说明输入的值在下拉框数据中不存在
                    for (var i = 0; i < allData.length; i++) {
                        if (val == allData[i][valueField]) {
                            result = false;
                        }
                    }
                    if (result) {
                        $(this).combobox("clear");
                    }
                },
                onSelect: function (data) {
                    $("#wageInfoForm input[name='carPay']").val(data.carPay);
                    initUserPayablewage();
                    initUserIncometax();
                    initUserNetpayroll();
                }
            });
        }
    });
    $("#wageInfo #userName").combobox({
        panelHeight: 'auto',
        prompt: '---请选择---'
    });
</script>
</body>
</html>
